/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package questionaire.resources;

import common.Laptop;
import inferenceEngine.labels.Label;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import process.ProcessManager;

/**
 *
 * @author aqeel
 */
public class Suggestions extends javax.swing.JFrame implements MouseListener
{
    static Questionaire questionaire = new Questionaire();
    private static Map<Integer,Integer> optionClicked = new LinkedHashMap<Integer,Integer>();
    private static Map<Integer, List> options = new LinkedHashMap<Integer,List>();
    private static Map<Integer,Map> inferenceMap = new LinkedHashMap<Integer,Map>();
    public static ArrayList array = new ArrayList();
    ProcessAnswer processAnswer = new ProcessAnswer();
    ProcessManager processManager;
    Laptop laptop = null;
    static List recommededList;
    static List requiredList;
    static List firedList;
    private int minimumPrice = 0;
    private int maximumPrice = 0;
    /**
     * Creates new form Results
     */
    public Suggestions(Questionaire questionaire,Map optionClicked,Map options,Map inferenceMap, int minimumPrice, int maximumPrice)
    {
        this.optionClicked = optionClicked;
        this.options = options;
        this.questionaire = questionaire;
        this.inferenceMap = inferenceMap;
        this.minimumPrice = minimumPrice;
        this.maximumPrice = maximumPrice;
        questionaire.setVisible(false);
        initComponents();
        init();
    }
    
    public void init()
    {
        JPanel jPanel = new JPanel();
        this.setBackground( Color.WHITE );
        jPanel.setBackground( Color.WHITE );
        setTitle("Results");
        setSize(900, 700);
        ImageIcon img = new ImageIcon(getClass().getResource("icon.png"));
        setIconImage(img.getImage());
        jButton1.setBackground(new Color(0,114,198));
        jButton1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Suggestions.this.setVisible(false);
                questionaire.clearAll();
                new Questionaire().setVisible(true);
            }
        });
        jLabel1.setFont(new Font(Font.SANS_SERIF,Font.BOLD,30));
        ArrayList<Label> labelList = processAnswer.returnLabels(optionClicked, inferenceMap);
        try
        {
            if(labelList!=null)
            {
                processManager = new ProcessManager();
                Collection<Laptop> suggestions = processManager.process(labelList, minimumPrice, maximumPrice);
                GridLayout gridLayout = new GridLayout(1,suggestions.size());
                jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
                gridLayout.setHgap(20);
                jPanel1.setLayout(gridLayout);
                if(suggestions!=null)
                {
                    firedList = processManager.getFiredRules();
                    recommededList = processManager.getRecommendedLabels();
                    requiredList = processManager.getRequiredLabels();
                    recommendedLabels.addMouseListener(this);
                    requiredLabels.addMouseListener(this);
                    firedLabels.addMouseListener(this);
                    for(Iterator suggestionIterator = suggestions.iterator();suggestionIterator.hasNext();)
                    {
                        JPanel suggestionPanel = new JPanel();
                        GridLayout resultLayout = new GridLayout(14, 2);
                        suggestionPanel.setLayout(resultLayout);
                        suggestionPanel.setBackground( Color.WHITE );
                        final Laptop laptop = (Laptop)suggestionIterator.next();
                        TitledBorder border = BorderFactory.createTitledBorder(laptop.getBrand() + " " + laptop.getName());
                        border.setTitleFont( new Font(Font.SANS_SERIF, Font.BOLD, 20));
                        suggestionPanel.setBorder( border );
                        suggestionPanel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
                        Font font = new Font(Font.SANS_SERIF,Font.PLAIN,14);
                        JLabel brandLabel = new JLabel("Brand :");
                        brandLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel cpuLabel = new JLabel("CPU");
                        cpuLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel ramLabel = new JLabel("RAM");
                        ramLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel gpuLabel = new JLabel("GPU");
                        gpuLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel graphicsmemoryLabel = new JLabel("Graphics Memory");
                        graphicsmemoryLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel hardiskmemoryLabel = new JLabel("Hard disk");
                        hardiskmemoryLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel screensizeLabel = new JLabel("Screen size");
                        screensizeLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel priceLabel = new JLabel("Price");
                        priceLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel recommendedMarkLabel = new JLabel("Recommended mark");
                        recommendedMarkLabel.setFont( font );
                        JLabel pricePerformanceLabel =  new JLabel("Price performance mark") ;
                        pricePerformanceLabel.setFont(font);
                        JLabel weightLabel =  new JLabel("Weight") ;
                        weightLabel.setFont(font);
                        JLabel thicknessLabel =  new JLabel("ThicknessLabel") ;
                        thicknessLabel.setFont(font);
                        
                        JLabel brand = new JLabel(": "+laptop.getBrand());
                        brand.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel cpu = new JLabel(": "+laptop.getCPU());
                        cpu.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel ram = new JLabel(": "+laptop.getRAM() + "G DDR3");
                        ram.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel gpu = new JLabel(": "+laptop.getGPU());
                        gpu.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel graphicsmemory = new JLabel(": "+laptop.getGraphicalMemory() + "G DDR5");
                        graphicsmemory.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel hardiskmemory = new JLabel(": "+laptop.getHDDSize() + "G");
                        hardiskmemory.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel screensize = new JLabel(": "+laptop.getScreenSize() + " inch");
                        screensize.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel price = new JLabel(": $"+laptop.getPrice() );
                        price.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel pricePeformance = new JLabel(": "+laptop.getPricePeformance() );
                        pricePeformance.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel batteryLife = new JLabel(": "+laptop.getBatterySize() + " h" );
                        batteryLife.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel thickness = new JLabel(": "+laptop.getThickness() + " inch" );
                        thickness.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel weight = new JLabel(": "+laptop.getWeight() + " lb" );
                        weight.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
                        JLabel recommendedMark = new JLabel(": "+ laptop.getRecommendMark() );
                        recommendedMark.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14));
//                        suggestionPanel.setFont( font);
                        suggestionPanel.add(brandLabel);
                        suggestionPanel.add(brand);
                        suggestionPanel.add(priceLabel);
                        suggestionPanel.add( price );
                        suggestionPanel.add( recommendedMarkLabel );
                        suggestionPanel.add(recommendedMark);
                        suggestionPanel.add( pricePerformanceLabel );
                        suggestionPanel.add(pricePeformance);
                        suggestionPanel.add(cpuLabel);
                        suggestionPanel.add(cpu);
                        suggestionPanel.add(ramLabel);
                        suggestionPanel.add(ram);
                        suggestionPanel.add(gpuLabel);
                        suggestionPanel.add(gpu);
                        suggestionPanel.add(graphicsmemoryLabel);
                        suggestionPanel.add(graphicsmemory);
                        suggestionPanel.add(hardiskmemoryLabel);
                        suggestionPanel.add(hardiskmemory);
                        suggestionPanel.add(screensizeLabel);
                        suggestionPanel.add(screensize);
                        suggestionPanel.add( weightLabel );
                        suggestionPanel.add(weight);
                        suggestionPanel.add( thicknessLabel );
                        suggestionPanel.add(thickness);
                        
                        /*JLabel requiredLabels = new JLabel("Required Label");
                        requiredLabels.setFont(new Font(Font.SANS_SERIF,Font.CENTER_BASELINE,14));
                        JLabel recommendedLabels = new JLabel("Recommended Label");
                        recommendedLabels.setFont(new Font(Font.SANS_SERIF,Font.CENTER_BASELINE,14));
                        JLabel firedLabels = new JLabel("Fired Rules");
                        firedLabels.setFont(new Font(Font.SANS_SERIF,Font.CENTER_BASELINE,14));
                        suggestionPanel.add(requiredLabels);
                        recommendedLabels.setForeground(Color.BLUE);
                        requiredLabels.setForeground(Color.BLUE);
                        firedLabels.setForeground(Color.BLUE);*/
                        /*suggestionPanel.add(new JLabel(""));
                        suggestionPanel.add(recommendedLabels);
                        suggestionPanel.add(new JLabel(""));
                        suggestionPanel.add(firedLabels);
                        suggestionPanel.add(new JLabel(""));*/
                        JButton tooHeavyButton = new JButton( "too heavy!" );
                        tooHeavyButton.addActionListener( new ActionListener() {
							
							@Override
							public void actionPerformed(ActionEvent arg0) {
								processManager.tooHeavy(laptop);								
							}
						});
                        JButton tooThickButton = new JButton( "too thick!");
                        tooThickButton.addActionListener( new ActionListener() {
							
							@Override
							public void actionPerformed(ActionEvent arg0) {
								processManager.tooThick(laptop);								
							}
						});
//                        JLabel like = new JLabel();
//                        ImageIcon icon = new ImageIcon(getClass().getResource("like.png"));
//                        like.setIcon(icon);
//                        like.addMouseListener(new MouseListener() {
//
//                            @Override
//                            public void mouseClicked(MouseEvent e){}
//                            @Override
//                            public void mousePressed(MouseEvent e)
//                            {
//                                processManager.likeLaptop(laptop);
//                                ImageIcon icon = new ImageIcon(getClass().getResource("success.png"));
//                                JOptionPane.showMessageDialog(rootPane, "Liked", "Success", JOptionPane.PLAIN_MESSAGE,icon);
//                            }
//                            @Override
//                            public void mouseReleased(MouseEvent e){}
//                            @Override
//                            public void mouseEntered(MouseEvent e){}
//                            @Override
//                            public void mouseExited(MouseEvent e){}
//                        });                        
//                        suggestionPanel.add(like);
//                        suggestionPanel.add(new JLabel(""));
                        suggestionPanel.add( tooHeavyButton );
                        suggestionPanel.add( tooThickButton );
                        jPanel1.add(suggestionPanel);
                        
                        
                        JLabel recommendAttributes = new JLabel("Inference Process");
                        recommendAttributes.setFont( new java.awt.Font("SansSerif", 0, 12) );
                        recommendAttributes.setForeground( Color.blue); 
                        recommendAttributes.addMouseListener( new MouseAdapter(){
                        	@Override
                            public void mousePressed(MouseEvent e)
                            {
                        		StringBuilder b = new StringBuilder();
                               for( String s : laptop.getFuzzyLogicProcess() ){
                            	   b.append( s + "\n" );
                               }
                               JOptionPane.showMessageDialog(rootPane, b.toString() );
                            }
						} );
                        

                        suggestionPanel.add(recommendAttributes);
                      suggestionPanel.add(new JLabel(""));
                        
                    }
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        requiredLabels = new javax.swing.JLabel();
        recommendedLabels = new javax.swing.JLabel();
        firedLabels = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Results");

        jButton1.setText("Start Over");

        requiredLabels.setBackground(java.awt.Color.blue);
        requiredLabels.setFont(new java.awt.Font("SansSerif", 0, 12)); // NOI18N
        requiredLabels.setForeground(java.awt.Color.blue);
        requiredLabels.setText("Required Labels");

        recommendedLabels.setFont(new java.awt.Font("SansSerif", 0, 12)); // NOI18N
        recommendedLabels.setForeground(java.awt.Color.blue);
        recommendedLabels.setText("Recommended Labels");

        firedLabels.setFont(new java.awt.Font("SansSerif", 0, 12)); // NOI18N
        firedLabels.setForeground(java.awt.Color.blue);
        firedLabels.setText("Fired Rules");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 396, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 285, Short.MAX_VALUE)
        );
        jScrollPane1.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setBackground( Color.WHITE);
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(requiredLabels)
                        .addGap(49, 49, 49)
                        .addComponent(recommendedLabels)
                        .addGap(47, 47, 47)
                        .addComponent(firedLabels)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(requiredLabels)
                    .addComponent(recommendedLabels)
                    .addComponent(firedLabels))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[])
//    {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(Suggestions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(Suggestions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(Suggestions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(Suggestions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable()
//        {
//            @Override
//            public void run()
//            {
//                new Suggestions(questionaire,optionClicked,options,inferenceMap,0,0).setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel firedLabels;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel recommendedLabels;
    private javax.swing.JLabel requiredLabels;
    // End of variables declaration//GEN-END:variables

    @Override
    public void mouseClicked(MouseEvent e)
    {
        /*JLabel jLabel = (JLabel)e.getSource();
        if(jLabel.getText().equals("Required Label"))
        {
            new Results(requiredList, "Required Labels");
        }
        else if(jLabel.getText().equals("Recommended Label"))
        {
            new Results(recommededList, "Recommended Labels");
        }
        else if(jLabel.getText().equals("Fired Rules"))
        {
            new Results(firedList, "Fired Rules");
        }*/
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        JLabel jLabel = (JLabel)e.getSource();
        if(jLabel.getText().equals("Required Labels"))
        {
            new Results(requiredList, "Required Labels");
        }
        else if(jLabel.getText().equals("Recommended Labels"))
        {
            new Results(recommededList, "Recommended Labels");
        }
        else if(jLabel.getText().equals("Fired Rules"))
        {
            new Results(firedList, "Fired Rules");
        }
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {}

    @Override
    public void mouseEntered(MouseEvent e)
    {}

    @Override
    public void mouseExited(MouseEvent e)
    {}
}
